﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BoAddonBiz_FreightForwarder.ObjectFind
{
    class B1Form_ConditionForm : SAPBoAddon.B1Form
    {
        public static ObjectFind Find { set; get; }
        #region override
        protected override bool BeforeSaveCheckFormData()
        {
            return true;
        }

        protected override void FormEditModeChange(SAPBoAddon.frmItemEditMode frmEditMode)
        {

        }

        protected override void FormInfoSetting(SAPBoAddon.B1FormSetting FormSetting)
        {
            this.FormSetting.FormType = "AVA_ObjectFind";
            this.FormSetting.FormFileName = "AVA_ConditionForm.srf";
        }

        protected override void InitializeForm(SAPBoAddon.B1FormInitializePar InitPar)
        {

        }
        #endregion
        #region constructor
        public B1Form_ConditionForm()
        {
            this.frm_ItemEvent += new SAPBoAddon.IB1FormEvents.B1ItemEventEventHandler(B1Form_ConditionForm_frm_ItemEvent);
        }

        void B1Form_ConditionForm_frm_ItemEvent(SAPBoAddon.B1AddonBase.B1ItemEvent pVal, ref bool BubbleEvent)
        {
            if (!pVal.BeforeAction)
            {
                if (pVal.ItemUID == "ok" && pVal.EventType == SAPbouiCOM.BoEventTypes.et_CLICK)
                {
                    #region 获取值
                    for (int i = 0; i < CurrentFormEx.DataSources.UserDataSources.Count; i++)
                    {
                        string UID = CurrentFormEx.DataSources.UserDataSources.Item(i).UID;

                        string Value = string.Empty;
                        switch (CurrentFormEx.DataSources.UserDataSources.Item(i).DataType)
                        {
                            case SAPbouiCOM.BoDataType.dt_DATE:
                                Value = CurrentFormEx.DataSources.UserDataSources.Item(i).ValueEx;
                                if (!string.IsNullOrEmpty(Value))
                                    Value = Value.Substring(0, 4) + "-" + Value.Substring(4, 2) + "-" + Value.Substring(6, 2);
                                break;
                            default:
                                Value = CurrentFormEx.DataSources.UserDataSources.Item(i).ValueEx;
                                break;
                        }
                        if (UID.Substring(0, 1) == "F")
                        {
                            Find.Lines.Wherefields.Getfield(UID.Substring(2, UID.Length - 2)).FValue = Value;
                            if (string.IsNullOrEmpty(CurrentFormEx.DataSources.UserDataSources.Item(i).ValueEx))
                            {
                                Find.Lines.Wherefields.Remove(UID.Substring(2, UID.Length - 2), WhereValue.left);
                            }
                            else
                                Find.Lines.Wherefields.Remove(UID.Substring(2, UID.Length - 2), WhereValue.no);
                        }
                        if (UID.Substring(0, 1) == "T")
                        {
                            Find.Lines.Wherefields.Getfield(UID.Substring(2, UID.Length - 2)).TValue = Value;
                            if (string.IsNullOrEmpty(CurrentFormEx.DataSources.UserDataSources.Item(i).ValueEx))
                            {
                                if (Find.Lines.Wherefields.Getfield(UID.Substring(2, UID.Length - 2)).DeleteValue == WhereValue.left)
                                    Find.Lines.Wherefields.Remove(UID.Substring(2, UID.Length - 2), WhereValue.all);
                                else
                                    Find.Lines.Wherefields.Remove(UID.Substring(2, UID.Length - 2), WhereValue.right);
                            }
                        }
                    }
                    #endregion
                    SAPbouiCOM.Form oForm = SAPBoAddon.B1Assistant.Form.LoadToSBOApplciation(System.Windows.Forms.Application.StartupPath + @"\Form\AVA_FindResultForm.srf", "");
                    #region 显示数据
                    SAPbouiCOM.DataTable dt;
                    dt = oForm.DataSources.DataTables.Add("result");
                    string SQL = Find.Lines.SQL.ToString();
                    dt.ExecuteQuery(SQL);
                    //Log.GetInstance().Write("查询语句：\r\n        " + SQL); /*去掉写日志 12-7-23 by WangPeng*/
                    SAPbouiCOM.Grid oGrid = oForm.Items.Item("Grid").Specific;
                    oGrid.DataTable = dt;
                    oGrid.Columns.Item(0).Visible = false;
                    oForm.DataSources.UserDataSources.Item("Counter").Value = oGrid.DataTable.Rows.Count.ToString();
                    #endregion
                }
                if (pVal.ItemUID == "setting" && pVal.EventType == SAPbouiCOM.BoEventTypes.et_CLICK)
                {
                    SAPBoAddon.B1FormInitializePar init = new SAPBoAddon.B1FormInitializePar();
                    init.CallType = SAPBoAddon.B1FormCallType.ct_FormItemCall;
                    init.FatherFormUID = SBOApp.Forms.ActiveForm.UniqueID;
                    init.KeyValue = Find.ObjectCode;
                    init.parObject = true;
                    SBOApp.Forms.ActiveForm.Close();
                    this.ShowForm(B1FormTypes.ado_AVA_ObjectFind, init);
                }
            }
        }
        #endregion
    }
}
